﻿using System.Collections.Generic;
using System.Configuration;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;

namespace BLL
{
    public class ClasseFoto
    {

        private int _alt { get; set; }
        private int _lar { get; set; }
        private int _qld { get; set; }
        private string _strPathImg { get; set; }
        
        private byte[] _img { get; set; }

        private int numfotosPorPasta = 10;

        //public tbFoto ft { get; set; }

        public int idFoto { get; set; }
        public int idObjeto { get; set; }
        public int intTnn { get; set; }

        public ClasseFoto(int alt, int lar, int qld)
        {
            _alt = alt;
            _lar = lar;
            _qld = qld;
            
        }


        public void _getImagem(byte[] img)
        {
            _img = img;
        }

        public int _dir(string _strCaminho)
        {
            
            string strPath = _strCaminho + ConfigurationManager.AppSettings["strDirImg"];

            _strPathImg = strPath;

            var fileCount = (from file in Directory.EnumerateFiles(@""+strPath, "*.*")
                             select file).Count();
            return fileCount;
            // var distinctCompanies = te.TbProdutoFoto.GroupBy(c => c.idProduto).Select(g => g.FirstOrDefault()).Count();
            //return distinctCompanies;


        }
        /*
         System.IO.MemoryStream MS = new System.IO.MemoryStream();
	long lngId = 0;
	string strFile = null;
	int intWidth = 0;
	int intHeight = 0;
	int intPerc = 0;
	System.Drawing.Image oImg = null;
	System.Drawing.Image oThumbnail = null;
	string strExt = null;

	BLL.Tabelas.Imagem oImagem = new BLL.Tabelas.Imagem(FormBase.IdUsuarioSessao(this.Context));
	DAL.Tabelas.dsImagemSel ds = default(DAL.Tabelas.dsImagemSel);

	lngId = Convert.ToInt64(Conversion.Val(Request("id")));
	intWidth = Convert.ToInt32(Conversion.Val(Request("w")));

	ds = oImagem.Ler(lngId);

	if (ds.Imagem.Rows.Count == 0) {
		return;
	}

	strExt = ds.Imagem(0).txtExtensao;

	string strTipoConteudo = TipoConteudo(strExt).Split("/")(0);

	if (strTipoConteudo != "image") {
		System.IO.MemoryStream fs = new System.IO.MemoryStream(ds.Imagem(0).Imagem);

		byte[] btFile = new byte[fs.Length + 1];
		fs.Read(btFile, 0, fs.Length);
		fs.Close();

		string strNomeArq = string.Format("{0:ddMMyyyy}", System.DateTime.Now);

		var _with1 = Response;
		_with1.AddHeader("Content-disposition", "attachment;filename=" + strNomeArq + Strings.Right(strExt, 3).ToUpper() + strExt);
		_with1.AddHeader("Content-Length", btFile.Length.ToString());
		_with1.ContentType = "application/octet-stream";
		_with1.BinaryWrite(btFile);
		_with1.End();

	} else {
		oImg = System.Drawing.Image.FromStream(new System.IO.MemoryStream(ds.Imagem(0).Imagem));

		Response.Clear();
		Response.ContentType = TipoConteudo(strExt);


		if (intWidth == 0) {
			Response.BinaryWrite(ds.Imagem(0).Imagem);
			return;

			intWidth = oImg.Width;

		}

		intPerc = Convert.ToInt32((intWidth * 100) / oImg.Width);
		intHeight = Convert.ToInt32(oImg.Height * intPerc / 100);

		oThumbnail = oImg.GetThumbnailImage(intWidth, intHeight, new System.Drawing.Image.GetThumbnailImageAbort(MyCallbackDelegate), System.IntPtr.Zero);

		oThumbnail.Save(MS, FormatoExtensao(strExt));

		Response.BinaryWrite(MS.ToArray());

		MS.Close();
		oThumbnail.Dispose();
		oImg.Dispose();

	}
         
         */

        public void Salvar(bool proporcional = false)
        {
            
            //int imageHeight = Convert.ToInt32(350);
            //int imageWidth = Convert.ToInt32(450);

            var stream = new MemoryStream(_img);
            var imgPhoto = Image.FromStream(stream);
            stream.Dispose();

            var bmPhoto = new Bitmap(_lar, _alt, PixelFormat.Format32bppRgb);
            bmPhoto.SetResolution(10, 10);
            Graphics grPhoto = Graphics.FromImage(bmPhoto);
            grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
            grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
            grPhoto.PixelOffsetMode = PixelOffsetMode.Default;
            grPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, _lar, _alt), 0, 0, imgPhoto.Width, imgPhoto.Height, GraphicsUnit.Pixel);

            string strNomeFoto = _strPathImg + "\\" + idFoto + "_" + idObjeto + "_" + intTnn + ".png";

            bmPhoto.Save(strNomeFoto, ImageFormat.Png);
            
            //var thumbNailImg = imgPhoto.GetThumbnailImage(imageWidth, imageHeight,
            //                                      null, IntPtr.Zero);
            //thumbNailImg.Save(_strPathImg + "\\"+ pf.idProdutoFoto  +"_" + pf.idProduto+"_"+pf.tmn+".png", ImageFormat.Png);
            //thumbNailImg.Dispose();

            imgPhoto.Dispose();
            bmPhoto.Dispose();
            grPhoto.Dispose();

            
            
        }

    }
}



